<?php

class HTech_Controller_Action extends Zend_Controller_Action
{
    protected $_model;    
    protected $_cfg;
    
    public function init($cfg)
    {
        $this->_helper->contextSwitch()
            ->addContext('csv', array('suffix' => 'csv'))
            ->addContext('prn', array('suffix' => 'prn'))
            ->addContext('mob', array('suffix' => 'mob'))
            ->addActionContext('browse', array('xml','csv','prn','mob'))
            ->initContext();
        if(isset($cfg['actionContext'][$this->_request->action]))
            $this->_helper->contextSwitch()->addActionContext($this->_request->action, 
                $cfg['actionContext'][$this->_request->action])->initContext();
        $this->view->addScriptPath(APPLICATION_PATH . "/layouts/scripts/");
        
        $config = array();
        if(isset($cfg['config'][$this->_request->action])) {
            $config = array('name' => $cfg['config'][$this->_request->action]);
        }
        try {
            $this->_model = new $cfg['model']($config);
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        if(!isset($cfg['redirect'])) $cfg['redirect'] = null;
        if(!isset($cfg['flashMessenger'])) $cfg['flashMessenger'] = null;
        $this->_cfg = $cfg;
    }
    
    function postDispatch()
    { 
        parent::postDispatch();
        switch ($this->_request->getParam('format')) {
            case 'csv': $this->_helper->layout->setLayout('csv'); break;
            case 'prn': $this->_helper->layout->setLayout('print'); break;
            default: break;
        }
    }
    
    public function browseAction()
    {
        $this->_helper->Browse->setModel($this->_model)->process();
    }
    
    public function logAction()
    {        
        $this->_helper->Browse->setModel($this->_model)->process();
    }
    
    public function addAction()
    {
        $this->view->form = new $this->_cfg['form']();
        $this->_helper->Add->setModel($this->_model)->setForm($this->view->form)
                ->setRedirect($this->_cfg['redirect'])
                ->setFlashMessenger($this->_cfg['flashMessenger'])
                ->process();  
        $this->_helper->layout->setLayout('form');
    }
    
    public function viewAction()
    {   
        $id = (int) $this->_request->getParam('id');
        $m = $this->_cfg['model'];
        try {
            $this->view->$m = $this->_model->getRecord($id);  
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $layout = isset($this->_cfg['layout']['view']) ? $this->_cfg['layout']['view'] : 'view';
        $this->_helper->layout->setLayout($layout);
    }
           
    public function mapAction()
    {        
        $id = (int) $this->_request->getParam('id');
        try {
            $this->view->points = $this->_model->getPoints($id);  
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->_helper->layout->setLayout('gmap');
    }
    
    public function performanceChartAction()
    {        
        $id = (int) $this->_request->getParam('id');
        try {
            $charts = $this->_model->getPerformanceCharts($this->_model->getRecord($id)->d);
            $this->view->charts = $charts['charts'];
            $this->view->title = $charts['title'];
            $this->view->subtitle = $charts['subtitle'];
            $this->view->params = $charts['params'];
            $this->view->request = $this->_request;
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->_helper->layout->setLayout('chart');
    }
}
